ALTER PROC SP_CREATE_UPDATE_SORT_BIN_INFO 
				@PART_NUMBER VARCHAR(50),
				@LOT_NUMBER VARCHAR(50),
				@WAFER_ID INT,
				@DATE_IN DATETIME,
				@PROBE_CARD VARCHAR(50),
				@TEMP	 VARCHAR(20),	
				@Probe_ID VARCHAR(50),
				@Insertion VARCHAR(50),
				@GROSS_DIE INT,
				@GOOD_DIE INT,
				@YIELD FLOAT,
				@VENDOR_NAME VARCHAR(50),
				@DATE_OUT DATETIME,
				@TEST_TIME VARCHAR(50),
				@VENDOR_LOT VARCHAR(50),
				@Bin_1 INT, @Bin_2 INT, @Bin_3 INT, @Bin_4 INT, @Bin_5 INT, @Bin_6 INT, @Bin_7 INT, @Bin_8 INT,	@Bin_9 INT, @Bin_10 INT,
				@Bin_11 INT,@Bin_12 INT,@Bin_13 INT,@Bin_14 INT,@Bin_15 INT,@Bin_16 INT,@Bin_17 INT,@Bin_18 INT,@Bin_19 INT,@Bin_20 INT,
				@Bin_21 INT,@Bin_22 INT,@Bin_23 INT,@Bin_24 INT,@Bin_25 INT,@Bin_26 INT,@Bin_27 INT,@Bin_28 INT,@Bin_29 INT,@Bin_30 INT,
				@Bin_31 INT,@Bin_32 INT,@Bin_33 INT,@Bin_34 INT,@Bin_35 INT,@Bin_36 INT,@Bin_37 INT,@Bin_38 INT,@Bin_39 INT,@Bin_40 INT,
				@Bin_41 INT,@Bin_42 INT,@Bin_43 INT,@Bin_44 INT,@Bin_45 INT,@Bin_46 INT,@Bin_47 INT,@Bin_48 INT,@Bin_49 INT,@Bin_50 INT,
				@Bin_51 INT,@Bin_52 INT,@Bin_53 INT,@Bin_54 INT,@Bin_55 INT,@Bin_56 INT,@Bin_57 INT,@Bin_58 INT,@Bin_59 INT,@Bin_60 INT,
				@Bin_61 INT,@Bin_62 INT,@Bin_63 INT,@Bin_64 INT,@Bin_65 INT,@Bin_66 INT,@Bin_67 INT,@Bin_68 INT,@Bin_69 INT,@Bin_70 INT,
				@Bin_71 INT,@Bin_72 INT,@Bin_73 INT,@Bin_74 INT,@Bin_75 INT,@Bin_76 INT,@Bin_77 INT,@Bin_78 INT,@Bin_79 INT,@Bin_80 INT,
				@Bin_81 INT,@Bin_82 INT,@Bin_83 INT,@Bin_84 INT,@Bin_85 INT,@Bin_86 INT,@Bin_87 INT,@Bin_88 INT,@Bin_89 INT,@Bin_90 INT,
				@Bin_91 INT,@Bin_92 INT,@Bin_93 INT,@Bin_94 INT,@Bin_95 INT,@Bin_96 INT,@Bin_97 INT,@Bin_98 INT,@Bin_99 INT,@Bin_100 INT
AS
BEGIN
	DECLARE @PROCESS_STEP_ID INT
	DECLARE @LOCATION_ID INT
	DECLARE @LOT_ID		INT

	SELECT @PROCESS_STEP_ID = PROCESS_STEP.PROCESSSTEP_ID FROM PRODUCT,PROCESS_STEP WHERE PRODUCT.PART_NUMBER = @PART_NUMBER AND PS_TYPE_NAME = 'SORT' AND PRODUCT.PROCESSSTEP_ID = PROCESS_STEP.PROCESSSTEP_ID 
	SELECT @LOCATION_ID = LOCATION_ID FROM LOCATION WHERE L_TYPE = 'SORT' AND L_VENDOR_NAME = @VENDOR_NAME	   	
   	IF @PROCESS_STEP_ID IS NULL -- NEW PRODUCT OR PRODUCT NAME MISMATCH
   	 BEGIN
   		SELECT @PROCESS_STEP_ID = PROCESS_STEP.PROCESSSTEP_ID FROM PRODUCT_MAP_TABLE,PROCESS_STEP WHERE PRODUCT_MAP_TABLE.PART_NUMBER = @PART_NUMBER AND PS_TYPE_NAME='SORT' AND PRODUCT_MAP_TABLE.PROCESSSTEP_ID=PROCESS_STEP.PROCESSSTEP_ID
	 END
	IF ( @PROCESS_STEP_ID IS NULL OR @LOCATION_ID IS NULL )
	 BEGIN
		RETURN
	 END
	SELECT @LOT_ID = LOT_ID FROM LOT_TABLE WHERE PROCESSSTEP_ID = @PROCESS_STEP_ID AND LOCATION_ID = @LOCATION_ID AND LOT_NUMBER = @LOT_NUMBER
 	
	IF @LOT_ID IS NOT NULL
 	BEGIN
		IF ( (SELECT COUNT(*) FROM SORT_BINNING_INFO WHERE Lot_Id = @LOT_ID AND Wafer_ID = @WAFER_ID) = 0 ) --NEED TO INSERT
		BEGIN			
			INSERT INTO SORT_BINNING_INFO (Lot_Id, Wafer_Id, Date_In, DATE_Out,Test_Time,VENDOR_LOt,Probe_card, Temp,Probe_Id,Insertion, Gross_Die, Good_Dice, Yield, Bin_1, Bin_2, Bin_3, Bin_4, Bin_5, Bin_6, Bin_7, Bin_8, Bin_9, Bin_10, Bin_11, Bin_12, Bin_13, Bin_14, Bin_15, Bin_16, Bin_17, Bin_18, Bin_19, Bin_20, Bin_21, Bin_22, Bin_23, Bin_24, Bin_25, Bin_26, Bin_27, Bin_28, Bin_29, Bin_30, Bin_31, Bin_32, Bin_33, Bin_34, Bin_35, Bin_36, Bin_37, Bin_38, Bin_39, Bin_40,
							Bin_41, Bin_42, Bin_43, Bin_44, Bin_45, Bin_46, Bin_47, Bin_48, Bin_49, Bin_50, Bin_51, Bin_52, Bin_53, Bin_54, Bin_55, Bin_56, Bin_57, Bin_58, Bin_59, Bin_60, Bin_61, Bin_62, Bin_63, Bin_64, Bin_65, Bin_66, Bin_67, Bin_68, Bin_69, Bin_70, Bin_71, Bin_72, Bin_73, Bin_74, Bin_75,
							Bin_76, Bin_77, Bin_78, Bin_79, Bin_80,Bin_81, Bin_82, Bin_83, Bin_84, Bin_85, Bin_86, Bin_87, Bin_88, Bin_89, Bin_90, Bin_91, Bin_92, Bin_93, Bin_94, Bin_95, Bin_96, Bin_97, Bin_98, Bin_99, Bin_100, Timestamp) 
							VALUES (@LOT_ID, @WAFER_ID, @DATE_IN, @DATE_OUT,@TEST_TIME,@VENDOR_LOT,@PROBE_CARD,@TEMP, @Probe_ID, @Insertion, @GROSS_DIE, @GOOD_DIE, @YIELD, @Bin_1, @Bin_2, @Bin_3, @Bin_4, @Bin_5, @Bin_6, @Bin_7, @Bin_8, @Bin_9, @Bin_10, @Bin_11, @Bin_12, @Bin_13, @Bin_14, @Bin_15, @Bin_16, @Bin_17, @Bin_18, @Bin_19, @Bin_20, @Bin_21, @Bin_22, @Bin_23, @Bin_24, @Bin_25, @Bin_26, @Bin_27, @Bin_28, @Bin_29, @Bin_30, @Bin_31, @Bin_32, @Bin_33, @Bin_34, @Bin_35, @Bin_36, @Bin_37, @Bin_38, @Bin_39, @Bin_40,
							@Bin_41, @Bin_42, @Bin_43, @Bin_44, @Bin_45, @Bin_46, @Bin_47, @Bin_48, @Bin_49, @Bin_50, @Bin_51, @Bin_52, @Bin_53, @Bin_54, @Bin_55, @Bin_56, @Bin_57, @Bin_58, @Bin_59, @Bin_60, @Bin_61, @Bin_62, @Bin_63, @Bin_64, @Bin_65, @Bin_66, @Bin_67, @Bin_68, @Bin_69, @Bin_70, @Bin_71, @Bin_72, @Bin_73, @Bin_74, @Bin_75,
							@Bin_76, @Bin_77, @Bin_78, @Bin_79, @Bin_80, @Bin_81, @Bin_82, @Bin_83, @Bin_84, @Bin_85, @Bin_86, @Bin_87, @Bin_88, @Bin_89, @Bin_90, @Bin_91, @Bin_92, @Bin_93, @Bin_94, @Bin_95, @Bin_96, @Bin_97, @Bin_98, @Bin_99, @Bin_100, CURRENT_TIMESTAMP)
		END
		ELSE --NEED TO UPDATE
		BEGIN
			UPDATE SORT_BINNING_INFO SET  Date_In = @DATE_IN,  DATE_Out = @DATE_OUT, Test_Time= @TEST_TIME, VENDOR_LOt = @VENDOR_LOT, Probe_card = @PROBE_CARD, Temp=@TEMP,Probe_Id = @Probe_ID,  Insertion = @Insertion,  Gross_Die = @GROSS_DIE,  Good_Dice = @GOOD_DIE,  Yield = @YIELD, 
					Bin_1 = @Bin_1,  Bin_2 = @Bin_2,  Bin_3 = @Bin_3,  Bin_4 = @Bin_4,  Bin_5 = @Bin_5,  Bin_6 = @Bin_6,  Bin_7 = @Bin_7,  Bin_8 = @Bin_8,  Bin_9 = @Bin_9,  Bin_10 = @Bin_10,  Bin_11 = @Bin_11,  Bin_12 = @Bin_12,  Bin_13 = @Bin_13,  Bin_14 = @Bin_14,  Bin_15 = @Bin_15,  Bin_16 = @Bin_16,  Bin_17 = @Bin_17,  Bin_18 = @Bin_18,  Bin_19 = @Bin_19,  Bin_20 = @Bin_20,  Bin_21 = @Bin_21,  Bin_22 = @Bin_22,  Bin_23 = @Bin_23,  Bin_24 = @Bin_24,  Bin_25 = @Bin_25,  Bin_26 = @Bin_26,  Bin_27 = @Bin_27,  Bin_28 = @Bin_28,  Bin_29 = @Bin_29,  Bin_30 = @Bin_30,  Bin_31 = @Bin_31,  Bin_32 = @Bin_32,  Bin_33 = @Bin_33,  Bin_34 = @Bin_34,  Bin_35 = @Bin_35,  Bin_36 = @Bin_36,  Bin_37 = @Bin_37,  Bin_38 = @Bin_38,  Bin_39 = @Bin_39,  Bin_40 = @Bin_40,
					Bin_41 = @Bin_41,  Bin_42 = @Bin_42,  Bin_43 = @Bin_43,  Bin_44 = @Bin_44,  Bin_45 = @Bin_45,  Bin_46 = @Bin_46,  Bin_47 = @Bin_47,  Bin_48 = @Bin_48,  Bin_49 = @Bin_49,  Bin_50 = @Bin_50,  Bin_51 = @Bin_51,  Bin_52 = @Bin_52,  Bin_53 = @Bin_53,  Bin_54 = @Bin_54,  Bin_55 = @Bin_55,  Bin_56 = @Bin_56,  Bin_57 = @Bin_57,  Bin_58 = @Bin_58,  Bin_59 = @Bin_59,  Bin_60 = @Bin_60,  Bin_61 = @Bin_61,  Bin_62 = @Bin_62,  Bin_63 = @Bin_63,  Bin_64 = @Bin_64,  Bin_65 = @Bin_65,  Bin_66 = @Bin_66,  Bin_67 = @Bin_67,  Bin_68 = @Bin_68,  Bin_69 = @Bin_69,  Bin_70 = @Bin_70,  Bin_71 = @Bin_71,  Bin_72 = @Bin_72,  Bin_73 = @Bin_73,  Bin_74 = @Bin_74,  Bin_75 = @Bin_75,  Bin_76 = @Bin_76,  Bin_77 = @Bin_77,  Bin_78 = @Bin_78,  Bin_79 = @Bin_79,  Bin_80 = @Bin_80,
					Bin_81 = @Bin_81,  Bin_82 = @Bin_82,  Bin_83 = @Bin_83,  Bin_84 = @Bin_84,  Bin_85 = @Bin_85,  Bin_86 = @Bin_86,  Bin_87 = @Bin_87,  Bin_88 = @Bin_88,  Bin_89 = @Bin_89,  Bin_90 = @Bin_90,  Bin_91 = @Bin_91,  Bin_92 = @Bin_92,  Bin_93 = @Bin_93,  Bin_94 = @Bin_94,  Bin_95 = @Bin_95,  Bin_96 = @Bin_96,  Bin_97 = @Bin_97,  Bin_98 = @Bin_98,  Bin_99 = @Bin_99,  Bin_100 = @Bin_100, Timestamp = CURRENT_TIMESTAMP 
					WHERE Lot_Id = @LOT_ID AND Wafer_ID = @WAFER_ID
		END
	END
		
	 
END